package com.example.administrator.music;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2018/4/11.
 */

public class ChangeUsername extends AppCompatActivity implements View.OnClickListener {
    private ImageView back;
    private TextView oldname;
    private EditText newname;
    private Button queding;
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.changname);
        initview();

    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent in1=getIntent();
        String name=in1.getStringExtra("oldname");
        oldname.setText("原用户名："+name);
    }

    private void initview() {
        back= (ImageView) findViewById(R.id.changname_back);
        oldname= (TextView) findViewById(R.id.chang_oldusername);
        newname= (EditText) findViewById(R.id.chang_newusername);
        queding= (Button) findViewById(R.id.chang_queding);

        queding.setEnabled(false);
        back.setOnClickListener(this);
        queding.setOnClickListener(this);
        newname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String name= String.valueOf(s);
                if (name.equals("")){
                    queding.setEnabled(false);
                    queding.setBackgroundResource(R.drawable.huibutton);
                }else {
                    queding.setEnabled(true);
                    queding.setBackgroundResource(R.drawable.button);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.changname_back:
                finish();
                break;
            case R.id.chang_queding:
                String id=load("musicuserobjectId").trim();
                MusicPlayUser playuser=new MusicPlayUser();
                playuser.setUsername(String.valueOf(newname.getText()));
                playuser.setTheme(MainActivity.theme);
                playuser.setMode(MainActivity.playmode);
                playuser.update(id, new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e==null){
                            File file1 = new File("/data/data/com.example.administrator.music/files/musicusername");
                            if (file1.exists()){
                                file1.delete();
                            }
                            Toast.makeText(ChangeUsername.this,"用户名修改成功",Toast.LENGTH_SHORT).show();
                            saveMassage(String.valueOf(newname.getText()),"musicusername");
                            finish();
                        }else {
                            Toast.makeText(ChangeUsername.this,"用户名修改失败"+e,Toast.LENGTH_SHORT).show();
                        }
                    }
                });


        }
    }
    //读取文件，返回String类型
    public String load(String data) {
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder content = new StringBuilder();
        try {
            in = openFileInput(data);           //“data”为文件名
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                line= line + "\n";               //换行
                content.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content.toString();
    }
    public void saveMassage(String inputText,String textname) {//inputText为传入的要保存的数据
        FileOutputStream out = null;
        BufferedWriter writer = null;
        try {
            //textname为文件名，第二个参数为文件操作模式：文件已经存在，就往文件里面追加类容，不从新创建文件。
            out = openFileOutput(textname, Context.MODE_APPEND);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(inputText);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
